package com.github.cyf.controller;

import com.github.cyf.log.HttpLog;
import com.github.cyf.log.OperationLog;
import com.github.cyf.pojo.ParamsReq;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.text.MessageFormat;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author chenyifan
 * @create 2025-01-09 11:25
 */
@RestController
@RequestMapping("/aop")
public class LogController {

    // get 请求参数
    @GetMapping("/testParams1")
    @OperationLog(methodDesc = "测试方法1")
    @HttpLog
    public ResponseEntity<String> testParams1(String param1, Integer param2) {
        return ResponseEntity.ok(MessageFormat.format("param1:{0};param2:{1}", param1, param2));
    }

    // Spring默认使用无参构造函数来实例化对象，所以ParamsReq不能是接口、抽象类等特殊类。
    @GetMapping("/testParams2")
    @OperationLog(methodDesc = "测试方法2")
    @HttpLog
    public ResponseEntity<String> testParams2(ParamsReq paramsReq) {
        return ResponseEntity.ok(MessageFormat.format("param1:{0};param2:{1}", paramsReq.getParam1(), paramsReq.getParam2()));
    }

    // post 请求参数
    @PostMapping("/testParams3")
    @OperationLog(methodDesc = "测试方法3")
    @HttpLog
    public ResponseEntity<String> testParams3(ParamsReq paramsReq) {
        return ResponseEntity.ok(MessageFormat.format("param1:{0};param2:{1}", paramsReq.getParam1(), paramsReq.getParam2()));
    }

    @PostMapping("/testParams4")
    @OperationLog(methodDesc = "测试方法4")
    @HttpLog
    public ResponseEntity<String> testParams4(@RequestBody ParamsReq paramsReq) {
        return ResponseEntity.ok(MessageFormat.format("param1:{0};param2:{1}", paramsReq.getParam1(), paramsReq.getParam2()));
    }

    // 路径参数
    @GetMapping("/testParams5/{pathParam}")
    @OperationLog(methodDesc = "测试方法5")
    @HttpLog
    public ResponseEntity<String> testParams5(@PathVariable("pathParam") String pathParam) {
        return ResponseEntity.ok(MessageFormat.format("pathParam:{0};", pathParam));
    }

    // 日期参数，加上@DateTimeFormat注解即可
    @GetMapping("/testParams6")
    @OperationLog(methodDesc = "测试方法6")
    @HttpLog
    public ResponseEntity<String> testParams6(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime time) {
        return ResponseEntity.ok(MessageFormat.format("time:{0};", time));
    }

    // 列表参数
    // Spring默认使用无参构造函数来实例化对象，但是List是一个接口，没有无参构造函数，需要使用@RequestParam注解来指定参数名
    @GetMapping("/testParams7")
    @OperationLog(methodDesc = "测试方法7")
    @HttpLog
    public ResponseEntity<String> testParams7(@RequestParam("paramList") List<Integer> paramList) {
        return ResponseEntity.ok(MessageFormat.format("paramList:{0};", paramList));
    }

    // 文件上传
    // 由于MultipartFile类也是一个接口，所以这里也需要加上@RequestParam注解来指定参数名
    @PostMapping("/upload")
    @OperationLog(methodDesc = "文件上传")
    @HttpLog
    public ResponseEntity<String> uploadFile(@RequestPart("file") MultipartFile file) {
        if (file.isEmpty()) {
            return new ResponseEntity<>("File is empty", HttpStatus.BAD_REQUEST);
        }
        try {
            byte[] bytes = file.getBytes();
            return new ResponseEntity<>("File uploaded successfully", HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>("Failed to upload file", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

}
